/* eslint-disable camelcase */
/* eslint-disable new-cap */
import { UserConfigExport, ConfigEnv, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
import path from 'path';
// import viteSvgIcons from 'vite-plugin-svg-icons';
import viteCompression from 'vite-plugin-compression';
// import { viteMockServe } from 'vite-plugin-mock';
import AutoImport from 'unplugin-auto-import/vite';
// import { viteVConsole } from 'vite-plugin-vconsole';

const CWD = process.cwd();
export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  const { VITE_BASE_URL } = loadEnv(mode, CWD);
  return {
    base: '/agent', //  绝对url路径
    css: {
      // css预处理器
      preprocessorOptions: {
        less: {
          charset: false,
          additionalData: `
          @import (reference) "./src/style/variables.less";
          `,
        },
      },
    },
    plugins: [
      vue(),
      Components({
        resolvers: [
          VantResolver(),
        ],
        dts: 'src/components.d.ts',
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
      }),
      // viteSvgIcons({
      //   // Specify the icon folder to be cached
      //   iconDirs: [path.resolve(process.cwd(), 'src/icons')],
      //   // Specify symbolId format
      //   symbolId: 'icon-[dir]-[name]',
      // }),
      viteCompression(),
      // viteMockServe({
      //   // default
      //   mockPath: 'mock',
      //   localEnabled: command === 'serve',
      // }),
      AutoImport({
        imports: ['vue', 'vue-router', 'vuex'],
        // 可以选择auto-import.d.ts生成的位置，使用ts建议设置为'src/auto-import.d.ts'
        // dts: 'src/auto-import.d.ts'
      }),
      // viteVConsole({
      //   entry: [path.resolve('src/main.ts')],
      //   localEnabled: true,
      //   enabled: true,
      //   config: {
      //     // vconsole options
      //     maxLogNumber: 1000,
      //     theme: 'light',
      //   },
      // }),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'), // 设置别名
      },
    },
    build: {
      // cssCodeSplit: false, 
      minify: 'terser',
      terserOptions: {
        compress: {
          // drop_console: true,
          drop_debugger: true,
        },
      },
    },
    server: {
      host: '0.0.0.0',
      // port: 10087, // 设置服务启动端口号
      open: false, // 设置服务启动时是否自动打开浏览器
      cors: true, // 允许跨域

      // 设置代理，根据项目实际情况配置
      proxy: {
        '/apis': {
          target: VITE_BASE_URL,
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace('/apis/', '/'),
        },
      },
      hmr: true,
    },
  };

};
